﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;
using System.Net;
using Newtonsoft.Json;

namespace Alos.Repository
{
    public class ReArrangeRecordRepository : BaseRepository<ReArrangeRecord>, IReArrangeRecordRepository
    {
        /// <summary>
        /// 新增发布记录表
        /// </summary>
        /// <param name="id">ClassID,LessonID,ResourceID,UserID-教师ID</param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public bool CreateReArrangeRecord(ReArrangeRecord reArrangeRecord, string serviceIP)
        {
            BasicClassInfoRepository basicClassService = new BasicClassInfoRepository();
            List<dynamic> classinfo= basicClassService.GetClassById(reArrangeRecord.ClassID.ToString()).ToList();
            try
            {
                
                WebClient WebClientService = new WebClient();
                reArrangeRecord.ArrangeTime = DateTime.Now;
                reArrangeRecord.ClassName = classinfo[0].ClassName;
                reArrangeRecord.CutoffTime = DateTime.Now.AddMinutes(120);
                reArrangeRecord.GradeCode = classinfo[0].GradeCode;
                reArrangeRecord.GradeName = classinfo[0].GradeName;
                reArrangeRecord.ResourceType = "RE003";
                reArrangeRecord.Status = 0;

                byte[] PostData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(reArrangeRecord));
                byte[] ResponseByte = WebClientService.UploadData(serviceIP + "/AddReArrangeRecord", PostData);
                return true;
            }
            catch (Exception)
            {
                reArrangeRecord.ArrangeTime = DateTime.Now;
                reArrangeRecord.ClassName = classinfo[0].ClassName;
                reArrangeRecord.CutoffTime = DateTime.Now.AddMinutes(120);
                reArrangeRecord.GradeCode = classinfo[0].GradeCode;
                reArrangeRecord.GradeName = classinfo[0].GradeName;
                reArrangeRecord.ResourceType = "RE003";
                reArrangeRecord.Status = 0;
                reArrangeRecord.Insert();
            }
            return true;   
        }
        /// <summary>
        /// 修改发布记录表
        /// </summary>
        /// <param name="id">发布记录表ID</param>      
        /// <returns></returns>
        public bool UpdateReArrangeRecord(int id)
        {
            var sql = Sql.Builder.Append(@"UPDATE ReArrangeRecord SET Status=1 WHERE ID=@0", id);
            return DBContext.Execute(sql)>0?true:false;           
        }
        /// <summary>
        /// 根据ID查询发布记录表
        /// </summary>
        /// <param name="id">发布记录表id</param>      
        /// <returns></returns>
        public ReArrangeRecord GetReArrangeRecord(int LessonID, int teacherid)
        {
            var sql = Sql.Builder.Append(@"select * from ReArrangeRecord where LessonID=@0 and UserID=@1", LessonID,teacherid);
            return DBContext.Single<ReArrangeRecord>(sql);  
        }
    }
}
